package com.igeek.boot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.anno.WMSLoganno;
import com.igeek.boot.entity.Shelves;
import com.igeek.boot.result.Result;
import com.igeek.boot.service.IShelvesService;
import com.igeek.boot.service.IWarehousesService;
import com.igeek.boot.vo.PageQueryVO;
import com.igeek.boot.vo.ShelfVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */
@RestController
@RequestMapping("/wisdom_factory/shelves")
@Api(tags = "货架表接口")
@Slf4j
public class ShelvesController {

    @Autowired
    private IShelvesService shelvesService;

    @Autowired
    private IWarehousesService warehousesService;
    //分页查询货架表的信息
    @GetMapping("/findPage")
    @ApiOperation("按条件分页查询货架表的信息")
    public Result findPage(@ApiParam("分页条件") PageQueryVO pageQueryVO){
        Page<Shelves> page = shelvesService.findPage(pageQueryVO);
        //page.getRecords().stream().map(shelves -> {
        //    shelves.setWarehouseName(warehousesService.findOneById(shelves.getWarehouseId()).getWarehouseName());
        //    return shelves;
        //});
        List<Shelves> records = page.getRecords();
        List<Shelves> collect = records.stream().map(shelves -> {
            shelves.setWarehouseName(warehousesService.findOneById(shelves.getWarehouseId()).getWarehouseName());
            return shelves;
        }).collect(Collectors.toList());
        page.setRecords(collect);
        //log.info("findPage:{}", page);
        //System.out.println("page = " + page);
        if(!ObjectUtils.isEmpty(page)) return new Result(true,"查询成功",page);
        return new Result(false,"暂无数据");
    }

    //查询所有货架的信息
    @GetMapping("/findAll")
    @ApiOperation("查询所有货架的信息")
    public Result findAll(){
        List<Shelves> list = shelvesService.findAll();
        if(!CollectionUtils.isEmpty(list))return new Result(true,"查询成功",list);
        return new Result(false,"暂无数据");
    }

    //删除货架的信息
    @WMSLoganno(module = "货架",operation = "删除货架")
    @DeleteMapping("/delShelves/{id}")
    @ApiOperation("删除货架的信息")
    public Result delShelves(@PathVariable("id") Integer id){
        boolean flag = shelvesService.delShelves(id);
        if(flag) return new Result(true,"删除成功");
        return new Result(false,"货架上还滞留着未处理的物资，请先清空物资再删除");
    }

    //修改货架的基本信息
    @WMSLoganno(module = "货架",operation = "修改货架")
    @PutMapping("/updateShelvesBaseInfo")
    @ApiOperation("修改货架的基本信息")
    public Result updateShelvesBaseInfo(@RequestBody Shelves shelves){
        boolean flag = shelvesService.updateShelvesBaseInfo(shelves);
        if(flag) return new Result(true,"修改成功");
        return new Result(false,"修改失败,货架已经存在了");
    }

    //添加货架信息 -> 需要先添加货架的基本信息 指定货架归属的仓库
    @PostMapping("/addShelvesInfo")
    @WMSLoganno(module = "货架",operation = "添加货架")
    @ApiOperation("添加货架的信息")
    public Result addShelvesInfo(@RequestBody Shelves shelves){
        boolean b = shelvesService.addShelvesInfo(shelves);
        if(b) return new Result(true,"添加成功");
        return new Result(false,"添加失败");
    }

    //树型选择器的货架-物资查询
    @GetMapping("/findShelfAndAssets")
    @ApiOperation("树型选择器的货架-物资查询")
    public Result findAllShelfAndAssets(){
        List<ShelfVO> shelvesList = shelvesService.findAllShelfAndAssets();
        return new Result(true,"查询成功",shelvesList);
    }
}
